package com.linhh.mall.center.server.service;

import com.linhh.mall.center.api.MenuInfoService;
import com.linhh.mall.center.common.pojo.MenuInfo;
import com.linhh.mall.center.common.pojo.MenuMetaVo;
import com.linhh.mall.center.common.pojo.MenuVo;
import com.linhh.mall.center.common.pojo.RoleInfo;
import com.linhh.mall.center.server.mappers.MenuInfoMapper;
import org.apache.dubbo.common.utils.StringUtils;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;


import java.util.*;
import java.util.stream.Collectors;

/**
 * @Author linhh
 * @Date 2022 10 22 11 10
 **/
@Service
public class MenuInfoServiceImpl implements MenuInfoService {

    @Autowired
    private MenuInfoMapper menuInfoMapper;

    /**
     * 根据角色列表获取菜单行列表
     *
     * @param roleInfos
     * @return
     */
    @Override
    public List<MenuInfo> findByRoles(List<RoleInfo> roleInfos) {
        List<Long> roleIds = roleInfos.stream().map(i -> {
            Long role = i.getId();
            return role;
        }).collect(Collectors.toList());
        List<MenuInfo> list = menuInfoMapper.selectListByRoles(roleIds);
        return list;
    }

    @Override
    public HashMap<String, Object> buildTree(List<MenuInfo> menuInfos) {
        List<MenuInfo> trees = new ArrayList<>();
        Set<Long> ids = new HashSet<>();
        for (MenuInfo menuDto : menuInfos) {
            if (menuDto.getPid() == 0) {
                trees.add(menuDto);
            }
            for (MenuInfo it : menuInfos) {
                if (it.getPid().equals(menuDto.getId())) {
                    if (menuDto.getChildren() == null) {
                        menuDto.setChildren(new ArrayList<>());
                    }
                    menuDto.getChildren().add(it);
                    ids.add(it.getId());
                }
            }
        }
        HashMap<String, Object> map = new HashMap<>(2);
        if (trees.size() == 0) {
            trees = menuInfos.stream().filter(s -> !ids.contains(s.getId())).collect(Collectors.toList());
        }
        map.put("content", trees);
        map.put("totalElements", menuInfos.size());
        return map;
    }

    @Override
    public List<MenuVo> buildMenus(List<MenuInfo> menuInfos) {
        List<MenuVo> list = new LinkedList<>();
        menuInfos.forEach(menuDTO -> {
            if (menuDTO != null) {
                List<MenuInfo> menuDtoList = menuDTO.getChildren();
                MenuVo menuVo = new MenuVo();
                menuVo.setName(StringUtils.isNotEmpty(menuDTO.getComponentName()) ? menuDTO.getComponentName() : menuDTO.getName());
                // 一级目录需要加斜杠，不然会报警告
                menuVo.setPath(menuDTO.getPid() == 0 ? "/" + menuDTO.getPath() : menuDTO.getPath());
                menuVo.setHidden(menuDTO.getHidden());
                // 如果不是外链

                if (!menuDTO.getIFrame()) {
                    if (menuDTO.getPid() == 0) {
                        menuVo.setComponent(StringUtils.isEmpty(menuDTO.getComponent()) ? "Layout" : menuDTO.getComponent());
                    } else if (!StringUtils.isEmpty(menuDTO.getComponent())) {
                        menuVo.setComponent(menuDTO.getComponent());
                    }
                }
                menuVo.setMeta(new MenuMetaVo(menuDTO.getName(), menuDTO.getIcon(), !menuDTO.getCache()));
                if (menuDtoList != null && menuDtoList.size() != 0) {
                    menuVo.setAlwaysShow(true);
                    menuVo.setRedirect("noredirect");
                    menuVo.setChildren(buildMenus(menuDtoList));
                    // 处理是一级菜单并且没有子菜单的情况
                } else if (menuDTO.getPid() == 0) {
                    MenuVo menuVo1 = new MenuVo();
                    menuVo1.setMeta(menuVo.getMeta());
                    // 非外链
                    if (!menuDTO.getIFrame()) {
                        menuVo1.setPath("index");
                        menuVo1.setName(menuVo.getName());
                        menuVo1.setComponent(menuVo.getComponent());
                    } else {
                        menuVo1.setPath(menuDTO.getPath());
                    }
                    menuVo.setName(null);
                    menuVo.setMeta(null);
                    menuVo.setComponent("Layout");
                    List<MenuVo> list1 = new ArrayList<>();
                    list1.add(menuVo1);
                    menuVo.setChildren(list1);
                }
                list.add(menuVo);
            }
        });
        return list;
    }
}
